<?php

namespace WoXuanWang\ActionLog\Repositories;

use WoXuanWang\ActionLog\Models\ActionLog;
use zgldh\Scaffold\BaseRepository;

class ActionLogRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'type',
        'content',
        'ip',
        'user_agent'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return ActionLog::class;
    }

    public static function log($type, $moduleName, $user = null, $otherContent = null)
    {
        $user = $user?:\Auth::user();
        $browser = \Agent::browser();
        $version = \Agent::version($browser);
        $user_agent_info = $browser.'版本号：'.$version;

        $url = \Request::method().' '.\Request::url();
        $content = "[{$moduleName}][{$user->name}][{$url}][{$otherContent}]";
        $ip = \Request::getClientIp();
        $data = [
            'type'=> $type,
            'user_agent'=>$user_agent_info,
            'content'=>$content,
            'ip'=>$ip,
            'created_by'=>$user->id
        ];
        ActionLog::create($data);
    }
}
